//一. 导入express包
//二. 创建router路由对象
//三. 编写路由规则
//四. 导出路由对象
var express = require('express')
var router = express.Router()
const { getAll, getById, exec } = require('../db/index')

// 解构调试的的标记,将来会根据这个决定是否发送错误信息
const { debug } = require('../config/index')

//处理请求(路由)
/**
 * 获取所有代办
 * GET /todo  返回[{},{}]
 */
router.get('/', async function (req, res) {
  // 操作数据库三部曲
  //1,获取请求的参数
  //2,编写sql语句
  //3,执行sql语句,返回结果
  let sql = 'select * from vuetodos'

  try {
    const data = await getAll(sql)
    res.send({
      code: 0,
      message: '获取所有的代办成功',
      result: data,
    })
  } catch (error) {
    res.send({
      code: 100101,
      message: '获取所有代办失败',
      result: debug ? error : '',
    })
  }
})

//获取一个代办//get/todo/1 返回{}
router.get('/:id', async function (req, res) {
  //1,获取请求的参数
  const id = req.params.id
  //对参数的格式进行校验
  const reg = /^\d+$/ //数字出现一次或者多次,并且以数字开头和结尾
  if (reg.test(id) == false) {
    res.send({
      code: 100104,
      message: 'id必须为数字',
      result: '',
    })
    return
  }

  //2,编写sql语句
  let sql = `select id,content from vuetodos where id=${id}`
  console.log(sql)
  //3,执行sql语句,返回结果
  try {
    const data = await getById(sql)
    if (data != null) {
      res.send({
        code: 0,
        message: '获取单个代办成功',
        result: data,
      })
    } else {
      res.send({
        code: 100103,
        message: 'id对应的数据不存在',
        result: '',
      })
    }
  } catch (error) {
    res.send({
      code: 100102,
      message: '获取单个代办失败',
    })
  }
})

//新增代办
//获取一个代办//post/todo {content:"test"}  返回(新增的数据){}
router.post('/', async function (req, res) {
  //1,获取请求的参数
  const content = req.body.content
  console.log(content)
  //对请求的参数格式校验(不能为空或者null)
  if (!content) {
    //如果content是空,或者是null,取反的结果是true,就可以执行里面的代码
    res.send({
      code: 100106,
      message: 'content格式错误',
      result: '',
    })
    return
  }
  //2,编写sql语句
  let sql = `insert into vuetodos (content) values ('${content}')`
  //3,执行sql语句,返回结果
  //从插入的结果中解构出insertid
  try {
    var { insertId } = await exec(sql)
  } catch (error) {
    res.send({
      code: 100105,
      message: '添加代办失败',
      result: debug ? error : '',
    })
    return
  }
  // 重新编写SQL语句
  sql = `select id,content from vuetodos where id=${insertId} and completed = 0`
  //执行sql语句,返回结果
  try {
    const data = await getById(sql)
    res.send({
      code: 0,
      message: '添加代办成功',
      result: data,
    })
  } catch (error) {
    res.send({
      code: 100105,
      message: '添加代办失败',
      result: debug ? error : '',
    })
  }
})

// 修改代办  put/todo {content:"test-new"}  返回(修改的数据){}
router.put('/:id', async function (req, res) {
  //1,获取请求的参数
  const id = req.params.id
  const { content, completed } = req.body

  //2,编写sql语句
  let sql = `update vuetodos set content='${content}',completed='${completed}' where id = ${id}`
  console.log(sql)
  //3,执行sql语句,返回结果
  try {
    const data = await exec(sql)
    res.send({
      code: 0,
      message: '修改代办成功',
      result: {
        data,
      },
    })
  } catch (error) {
    res.send({
      code: 100107,
      message: '修改代办失败',
      result: debug ? error : '',
    })
  }
})

// 删除代办
router.delete('/:id', async function (req, res) {
  //1,获取请求的参数
  const id = req.params.id
  //2,编写sql语句
  let sql = `delete from vuetodos where id=${id}`
  //执行sql语句,返回结果
  try {
    const data = await exec(sql)
    res.send({
      code: 0,
      message: '删除代办成功',
      result: '',
    })
  } catch (error) {
    res.send({
      code: 100108,
      message: '修改代办失败',
      result: debug ? error : '',
    })
  }
})

// 删除代办//delete/todo?completed=1
router.delete('/', async function (req, res) {
  //1,获取请求的参数
  const { completed } = req.query
  console.log(completed)
  console.log('你好')
  //2,编写sql语句
  let sql = `delete from vuetodos where completed = ${completed}`
  //执行sql语句,返回结果
  try {
    const data = await exec(sql)
    res.send({
      code: 0,
      message: '删除代办成功',
      result: '',
    })
  } catch (error) {
    res.send({
      code: 100108,
      message: '修改代办失败',
      result: debug ? error : '',
    })
  }
})

module.exports = router
